#ifndef WEBSOCKET_SESSION_HANDLER_H
#define WEBSOCKET_SESSION_HANDLER_H

#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include "Poco/Exception.h"
#include "Poco/Logger.h"
#include <iostream>

class WebSocketSessionHandler : public Poco::Net::HTTPRequestHandler {
public:
    WebSocketSessionHandler(const std::string& targetHost, int targetPort, const std::string& targetPath);
    void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) override;

private:
    std::string _targetHost;
    int _targetPort;
    std::string _targetPath;

    // Helper class to proxy data between client and backend WebSockets
    class WebSocketProxyWorker : public Poco::Runnable {
    public:
        WebSocketProxyWorker(Poco::Net::WebSocket& clientWs, Poco::Net::WebSocket& backendWs);
        void run() override;
        void stop();

    private:
        Poco::Net::WebSocket& _clientWs;
        Poco::Net::WebSocket& _backendWs;
        bool _stopRequested;
        Poco::Logger& _logger;
    };
};

#endif // WEBSOCKET_SESSION_HANDLER_H